#include "control.h"



unsigned char yi, er = 17,
				 san = 17, 
				 si =  17, 
				 wu =  17, 
				 liu = 17, 
				 qi =  17, 
				 ba =  17;

unsigned char SMG_Show_Flag;
unsigned char PCF8591_Read_DataFlag;
unsigned char Frequency_ReadData_Flag;
unsigned int  Frequency_Data;
int Data;
float PCF_DataShow;	
unsigned char SMG_ENABLE_FLAG = 0;
unsigned char LED_ENABLE_FLAG = 0;

void Power_OR_Frequency_Jiance(void)
{
	//测电压
	if (PCF8591_Read_DataFlag == 1)	
	{
		Data = PCF8591_ReadByte(0X03);//滑动变阻器接AIN3通道
		PCF_DataShow = (float)Data * 5 /255; 
		PCF8591_Read_DataFlag = 0;
	}
	
	//读取频率
	if (Frequency_ReadData_Flag == 1)
	{
		TR0 = 0;
		Frequency_Data = TH0*256+TL0;
		Frequency_Data = Frequency_Data * 2;
		TL0=0;
		TH0=0;
		TR0=1;
		Frequency_ReadData_Flag = 0;
	}	
}

void Power_OR_Frequency_Show(void)
{
	//显示测量电压U界面
	if(SMG_Show_Flag == 0)	
	{
		yi = 28;
		san= 17;
		si = 17;	//熄灭码 17
		wu = 17;
		liu= ((int)PCF_DataShow%10) + 18;
		qi = (int)(PCF_DataShow*100)/10%10;
		ba = (int)(PCF_DataShow*100)%10;
		
		if (LED_ENABLE_FLAG != 1)
		{
			P2 = 0x80;	// 1000 0000
			P0 = 0xFE;	//L1点亮、L2熄灭
			P00 = 0;
			P01 = 1;
			if (PCF_DataShow < 1.5){P02 = 1;}
			else if (PCF_DataShow >= 1.5 && PCF_DataShow < 2.5){P02 = 0;}
			else if (PCF_DataShow >= 2.5 && PCF_DataShow < 3.5){P02 = 1;}
			else if(PCF_DataShow >= 3.5){P02 = 0;}
		}
		
	}
	//显示测量频率F界面
	else if (SMG_Show_Flag == 1)	
	{
		yi = 15;
		san= Frequency_Data/100000%10;
		si = Frequency_Data/10000%10;
		wu = Frequency_Data/1000%10;
		liu= Frequency_Data/100%10;
		qi = Frequency_Data/10%10;
		ba = Frequency_Data/1%10;
		
		if (Frequency_Data <= 99999 && Frequency_Data >= 10000)
			san = 17;
		else if(Frequency_Data <= 9999 && Frequency_Data >= 1000)
		{	san = 17; si = 17; }
		else if(Frequency_Data <= 999 && Frequency_Data >= 100)
		{	san = 17; si = 17; wu = 17;}
		else if(Frequency_Data <= 99 && Frequency_Data >= 10)
		{	san = 17; si = 17; wu = 17; liu = 17;}
		
		if (LED_ENABLE_FLAG != 1)
		{
			P2 = 0x80;	// 1000 0000
			P00 = 1;	//L1熄灭、L2点亮
			P01 = 0;	
			if(Frequency_Data < 1000){P03 = 1;}
			else if (Frequency_Data >= 1000 && Frequency_Data < 5000){P03 = 0;}
			else if (Frequency_Data >= 5000 && Frequency_Data < 10000){P03 = 1;}
			else if(Frequency_Data >= 10000){P03 = 0;}
		}
	}
}

void LED_ENABLE_ON_OR_OFF(void)
{
//	if (0 == LED_ENABLE_FLAG)
//	{
//		//不关闭LED
//	}
	if (1 == LED_ENABLE_FLAG)	//关闭LED
	{
		P2 = 0x80;	// 1000 0000
		P0 = 0xFF;	//offLED
	}
}

void SMG_ENABLE_ON_OR_OFF(void)
{
	//按键7关闭数码管
	if (SMG_ENABLE_FLAG == 0)
	{
//		P2 = 0xC0;	//使能锁存器U8 1100 0000
//		P0 = 0xFF;	//选择所有数码管
//		P2 = 0xFF;	//使能锁存器U7 1111 1111
////		P0 = 0xFF;	//关闭所有数码管
	}
	else if (SMG_ENABLE_FLAG == 1)
	{
//		P2 = 0xC0;	//使能锁存器U8 1100 0000
//		P0 = 0xFF;	//选择所有数码管
//		P2 = 0xFF;	//使能锁存器U7 1111 1111
//		P0 = 0xFF;	//关闭所有数码管
		yi = er = san = si = wu = liu = qi = ba = 17;	//熄灭码，全灭，但是没有关闭数码管！！！
		
	}
}

void SMG_ENABLE_OFF(void)
{

}
/***************************中断服务子函数***************************/
/*定时器1中断*/
void Timer1_Rountine(void) interrupt 3
{
	static unsigned int Duli_Key;
	static unsigned int PCF8591_Read_Vol;
	static unsigned int Frequency_Data_Count;	//频率 数据 计次变量
	
	Duli_Key++;
	if(Duli_Key >= 20)	//10ms扫描独立按键
	{		
		KeyScan();		//执行操作
		Duli_Key = 0;	//软件复位
	}
	
	PCF8591_Read_Vol ++;	
	if (PCF8591_Read_Vol >= 450)	//450ms
	{
		PCF8591_Read_DataFlag = 1;
		PCF8591_Read_Vol = 0;		
	}
	
	Frequency_Data_Count++;
	if(Frequency_Data_Count >= 950)	//950ms < 1S
	{
		//执行操作
		Frequency_ReadData_Flag = 1;
		Frequency_Data_Count = 0;	//软件复位
	}
}

/*定时器2中断*/
//12T 16位自动重载
void Timer2_Routine() interrupt 12
{
    	

//	Duli_Key ++;	
		
	
}
